package Collection;

import java.util.Arrays;
import java.util.List;

/**
 * 数组转集合
 */
public class ArrayToList {
    public static void main(String[] args) {
        //数组转集合，一定是list集，因为只有list集才有下标
        String[] array ={"one","two","three","four","five"};
        System.out.println("array:"+ Arrays.toString(array));//array:[one, two, three, four, five]

        //asList()方法会返回内部的ArrayList，内部直接引用给数组array
        List<String> list =Arrays.asList(array);
        System.out.println("list:"+list);//list:[one, two, three, four, five]
        System.out.println("------");

        //修改数组中元素数据后，集合与会相应的改变
        array[1]="six";
        System.out.println("array:"+ Arrays.toString(array));//array:[one, six, three, four, five]
        System.out.println("list:"+list);//list:[one, six, three, four, five]
        System.out.println("------");

        //修改集合元素数据后，数组与会相应的改变
        list.set(2,"seven");
        System.out.println("list:"+list);//list:[one, six, seven, four, five]
        System.out.println("array:"+Arrays.toString(array));//array:[one, six, seven, four, five]


        //给集合添加/删除元素相当于给数组添加/删除元素
        //而数组是定长的，不会自动扩容/缩容
        list.add("!!!");//运行时会发生不支持操作异常



    }
}
